const category_detail = require('../models/category_detail')

// 获取商品类别明细
const getCategoryDetails = async (ctx) => {
  const query = ctx.request.query
  const data = await category_detail.getCategoryDetails(query)
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 获取id获取商品类别明细
const getCategoryDetailById = async (ctx) => {
  const id = ctx.params.id
  const data = await category_detail.getCategoryDetailById(id)
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 添加商品明细
const addCategoryDetail = async (ctx) => {
  const data = ctx.request.body
  const result = await category_detail.addCategoryDetail(data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 修改商品明细
const updateCategoryDetail = async (ctx) => {
  const data = ctx.request.body
  const id = ctx.params.id
  const result = await category_detail.updateCategoryDetail(data, id)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据关键字获取商品类别明细
const getDetailByKeyWords = async (ctx) => {
  const keywords = ctx.request.query
  const data = await category_detail.getDetailByKeyWords(keywords)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 删除明细
const deleteCategoryDetail = async (ctx) => {
  const id = ctx.params.id
  const result = await category_detail.deleteCategoryDetail(id)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功',
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
module.exports = {
  getCategoryDetails,
  getCategoryDetailById,
  addCategoryDetail,
  updateCategoryDetail,
  getDetailByKeyWords,
  deleteCategoryDetail
}